/*
 * @Author: sunkai@xinxinapp.cn
 * @Date: 2021-06-24 10:48:44
 * @LastEditTime: 2021-06-24 10:51:13
 * @LastEditors: sunkai@xinxinapp.cn
 * @Description: 
 * @FilePath: /fed-e-task-05-01/code/03.js
 */
const http = require("http")
const fs = require("fs")
const url = require("url")
const path = require("path")

const PORT = 8000
const serverHandle = (req, res) => {
    const url = new URL(req.url)
    const filePath = path.join(__dirname, url.pathname)
    fs.readFile(filePath, "binary", (err, file) => {
        if (err) {
            res.writeHead(404, "not found")
            res.end("<h1>404 NOT FOUND</h1>")
        } else {
            res.write(file, "binary")
            res.end()
        }
    })
}

http.createServer(serverHandle).listen(PORT)
console.log(`正在监听${PORT}端口`)